package com.snow.controller.admin;

import com.snow.constant.Types;
import com.snow.constant.Webconst;
import com.snow.controller.BaseController;
import com.snow.pojo.dto.MetaDto;
import com.snow.service.meta.MetaService;
import com.snow.utils.AdminCommons;
import com.snow.utils.ApiResponse;
import com.snow.utils.Commons;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:
 * 分类页控制器
 * @author: snow
 * @Date: 2020-01-31
 **/
@Api("分类管理页")
@Controller
@RequestMapping("/admin/category")
public class CategoryController extends BaseController {
    private static Logger logger = LoggerFactory.getLogger(CategoryController.class);
    @Autowired
    private MetaService metaService;
    @Autowired
    private Commons commons;
    @Autowired
    private AdminCommons adminCommons;

    /**
     * adminCommons Commons,categories,tags
     * @param model
     * @return
     */
    @GetMapping("")
    @ApiOperation("进入标签管分类理页")
    public String categoryIndex(Model model){
        List<MetaDto> categories = metaService.getMetasByType(Types.CATEGORY.getType(), null, Webconst.MAX_POSTS);
        List<MetaDto> tags = metaService.getMetasByType(Types.TAG.getType(), null, Webconst.MAX_POSTS);
        model.addAttribute("tags",tags);
        model.addAttribute("categories",categories);
        logger.info("categories.length is {}",categories.size());
        logger.info("tags.length is{}" ,tags.size());
        model.addAttribute("adminCommons",adminCommons);
        model.addAttribute("commons",commons);

        return "admin/category";
    }
    @PostMapping("/save")
    @ResponseBody
    @ApiOperation("保存或修改分类")
    public ApiResponse saveCategory(
            @ApiParam(name="cname",value="分类名",required = true)
            @RequestParam(name="cname",required = true)
            String cname,
            @ApiParam(name="mid",value="分类id",required = false)
            @RequestParam(name="mid",required = false)
            Integer mid
    ){
        try{
            metaService.saveMeta(cname,Types.CATEGORY.getType(),mid);
        }catch (Exception e){
            e.printStackTrace();
            String msg = "保存失败！";
            logger.error("分类保存失败 Error in CategoryController.java 71 row");
            return ApiResponse.fail(msg);
        }
        return ApiResponse.success();
    }

    @ApiOperation("删除标签或分类")
    @PostMapping("/delete")
    @ResponseBody
    public ApiResponse delete(
            @ApiParam(name="mid",value="标签或分类的id",required = true)
            @RequestParam(name="mid",required = true)
            Integer mid
    ){
        try{
            metaService.deleteMetaById(mid);
        }catch (Exception e){
            e.printStackTrace();
            logger.error("删除失败！ Error in categoryController.java 91 row " + e.getMessage());
            return ApiResponse.fail("删除失败！可能是内部系统问题或已被删除！");
        }
        return ApiResponse.success();
    }

}
